package com.mmsj;

import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author ChuXuan
 */
public class ExternalActivity extends AppCompatActivity {

//    初始化控件
    EditText wEditText;
    Button wButton;
    Button rButton;
    TextView textView;
    private boolean isWrite;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_external);

        // 获取控件
        wEditText = findViewById(R.id.et_text);
        wButton = findViewById(R.id.btn_write);
        rButton = findViewById(R.id.btn_read);
        textView = findViewById(R.id.tv_text);
        //检查SD卡状态
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, "SD卡已挂载", Toast.LENGTH_SHORT).show();
            isWrite = true;
        }else {
            Toast.makeText(this, "SD卡未挂载", Toast.LENGTH_SHORT).show();
            isWrite = false;
        }

        // 给写入按钮添加点击事件
        wButton.setOnClickListener(v -> {
            String content = wEditText.getText().toString();
            if(isWrite){
                try {
                    //写入SD卡
                    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/myTest.txt";
                    // 打开文件
                    FileOutputStream fos = new FileOutputStream(path);
                    // 写入文件
                    fos.write(content.getBytes());
                    // 关闭文件
                    fos.close();
                    Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
        // 给读取按钮添加点击事件
        rButton.setOnClickListener(v -> {
           if(isWrite){
               try {
                    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/myTest.txt";
                    FileInputStream fis = new FileInputStream(path);
                    byte[] buffer = new byte[1024];
                    int len = fis.read(buffer);
                    String content = new String(buffer,0,len);
                    textView.setText(content);
                    fis.close();
                    Toast.makeText(this, "读取成功", Toast.LENGTH_SHORT).show();
               } catch (Exception e) {
                   throw new RuntimeException(e);
               }
           }
        });

    }
}